package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description TODO
 * @Author dingchunlin
 * @Date 2023/8/19 18:48
 * @Version 1.0.0
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //首先查询该套餐或者菜品是否已经存在，如果存在则修改数量及价格就返回
        ShoppingCart result = shoppingCartMapper.selectOne(userId, shoppingCartDTO.getDishId(), shoppingCartDTO.getSetmealId());
        if (result != null) {
            shoppingCartMapper.updateById(result.getId());
            return;
        }
        //给要添加的数据进行赋值
        shoppingCart.setUserId(userId);
        shoppingCart.setNumber(1);
        shoppingCart.setCreateTime(LocalDateTime.now());
        //设置添加购物车的价格
        if (shoppingCartDTO.getDishId() != null) {
            Long dishId = shoppingCartDTO.getDishId();
            Dish dish = dishMapper.selectById(dishId);
            shoppingCart.setAmount(dish.getPrice());
            shoppingCart.setName(dish.getName());
            shoppingCart.setImage(dish.getImage());
        }else if (shoppingCartDTO.getSetmealId() != null) {
            Long setmealId = shoppingCartDTO.getSetmealId();
            Setmeal setmeal = setmealMapper.selectSetmealById(setmealId);
            shoppingCart.setAmount(setmeal.getPrice());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setImage(setmeal.getImage());
        }
        shoppingCartMapper.save(shoppingCart);
    }

    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        //根据用户和菜品id查询购物车中的数据
        ShoppingCart shoppingCart = shoppingCartMapper.selectOne(userId, shoppingCartDTO.getDishId(), shoppingCartDTO.getSetmealId());
        //判断当前商品在购物车中的数量是否为1，要是为1则可以直接删除
        if (shoppingCart.getNumber()==1){
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }else {
            //要是数量不为1，则将数量进行减1
            shoppingCartMapper.subById(shoppingCart.getId());
        }
    }
}
